<?php

namespace app\admin\controller;


use app\admin\model\system\SystemAdmin;
use basic\SystemBasic;
use service\CacheService;
use service\UtilService;
use think\Request;
use think\Response;
use think\Session;
use think\Url;
use service\JsonService;

/**
 * 登录验证控制器
 * Class Login
 * @package app\admin\controller
 */
class Login extends SystemBasic
{
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 登录验证 + 验证码验证
     */
    public function verify(Request $request)
    {
        if(!$request->isPost()) return JsonService::fail('请输入登录信息');
        list($account,$pwd,$code) = UtilService::postMore([
            'account','pwd' , 'code'
        ],$request,true);

        //if ( !(new \think\captcha\Captcha)->check($code) ) return $this->failed('验证码错误');

        //检验帐号密码
        $res = SystemAdmin::login($account,$pwd);

        if($res){
            return $this->redirect(Url::build('Index/index'));
        }

        return $this->failed('用户名错误，请重新输入');
    }

    public function captcha()
    {
        ob_clean();
        $captcha = new \think\captcha\Captcha([
            'codeSet'=>'0123456789',
            'length'=>4,
            'fontSize'=>30
        ]);
        return $captcha->entry();
    }

    /**
     * 退出登陆
     */
    public function logout()
    {
        SystemAdmin::clearLoginInfo();
        $this->redirect('Login/index');
    }
}